package com.monochromebytes.mononet.test;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.monochromebytes.mononet.test.network.ClientNetworkController;

public class Client extends BasicGame {

    protected static int                  delta;
    private final SlickWindow             slickWindow;

    private final ClientNetworkController network;

    public Client(InetSocketAddress localAddress, InetSocketAddress serverAddress)
            throws SlickException, IOException {
        super("Client - " + localAddress + " - Server: " + serverAddress);
        this.network = new ClientNetworkController(localAddress, serverAddress);

        this.slickWindow = new SlickWindow(this);
    }

    @Override
    public void init(GameContainer container) throws SlickException {
        // Nothing to do.
    }

    @Override
    public void render(GameContainer container, Graphics g) {
        Body body = this.network.getBody();
        if (body != null) {
            body.render(g);
        }

        g.drawString("connected: " + this.network.isConnected(), 20, 480);
        g.drawString("sent:      " + this.network.getNumberOfSentPackets(), 20, 500);
        g.drawString("received:  " + this.network.getNumberOfReceivedPackets(), 20, 520);
        g.drawString("lost:      " + this.network.getNumberOfLostPackets(), 20, 540);
        g.drawString("ack:       " + this.network.getNumberOfAcknowledgedPackets(), 20, 560);
        g.drawString("latency:   " + this.network.getLatency() + "ms", 20, 580);
    }

    @Override
    public void update(GameContainer container, int delta) {
        this.network.update(delta);
    }

    public SlickWindow getSlickWindow() {
        return slickWindow;
    }
}
